package br.caixa2.reports;

import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;

import br.caixa2.entities.AccountImpl;
import br.caixa2.entities.Extract;
import br.caixa2.enums.TypeOperation;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public class ExtractListReport {
	private static Font fontTitulo = new Font( Font.HELVETICA, 16, Font.BOLDITALIC );

	public static void report( AccountImpl account ) throws IOException {
		File file = File.createTempFile( "accountReport", ".pdf" );
		try {
			Document document = new Document();
			PdfWriter.getInstance( document, new FileOutputStream( file ) );
			document.open();
			addTitlePage( document, String.format( "Extrato da conta de Nº %d\n", account.getNumber() ) );
			addTitlePage( document, String.format( "Proprietário %s\n", account.getClient().getName() ) );

			PdfPTable extractTable = new PdfPTable( 4 );
			extractTable.setWidthPercentage( 100 );
			extractTable.addCell( new PdfPCell( new Paragraph( "Data" ) ) );
			extractTable.addCell( new PdfPCell( new Paragraph( "Descrição" ) ) );
			extractTable.addCell( new PdfPCell( new Paragraph( "Tipo" ) ) );
			extractTable.addCell( new PdfPCell( new Paragraph( "Valor" ) ) );
			SimpleDateFormat f = new SimpleDateFormat( "dd/MM/yyyy" );

			for ( Extract extract : account.getExtract() ) {
				Color color = null;
				if ( extract.getType().equals( TypeOperation.DRAW ) )
					color = new Color( 255, 102, 52 );
				else if ( extract.getType().equals( TypeOperation.DEPOSIT ) )
					color = new Color( 51, 255, 102 );
				else if ( extract.getType().equals( TypeOperation.TRANSFER ) )
					color = new Color( 255, 219, 102 );
				extractTable.addCell( genCell( extract, f.format( extract.getDate() ), color ) );
				extractTable.addCell( genCell( extract, extract.getDescription(), color ) );
				extractTable.addCell( genCell( extract, extract.getType().toString(), color ) );
				extractTable.addCell( genCell( extract, String.format( "R$ %.2f", extract.getValue() ), color ) );
			}

			document.add( extractTable );
			document.close();
		} catch ( Exception e ) {
			e.printStackTrace();
		}

		java.awt.Desktop desktop = java.awt.Desktop.getDesktop();

		try {
			desktop.open( file );
		} catch ( IOException e ) {
			e.printStackTrace();
		}
	}

	private static PdfPCell genCell( Extract extract, String value, Color color ) {
		Paragraph paragraph = new Paragraph( value );
		PdfPCell pdfPCell = new PdfPCell( paragraph );
		pdfPCell.setBackgroundColor( color );
		return pdfPCell;
	}

	private static void addTitlePage( Document document, String title ) throws DocumentException {
		Paragraph preface = new Paragraph();
		preface.add( new Paragraph( title, fontTitulo ) );
		document.add( preface );
	}
}
